import Ajax from '../../common/Ajax';
import citys from '../../common/citys';
import moment from '../../common/moment';

let sessionId = wx.getStorageSync('sessionId');

Page({
    /**
     * 页面的初始数据
     */
    data: {
        personInfo: {},
        locationShow: false,
        areaList: citys,
        birthday: "",
        show: false,
        sex: "",
        sexShow: false,
        phoneShow: false,
        phone: "",
        nameShow: false,
        realName: "",
        minDate: new Date("1970-1-1").getTime(),
        formatter(type, value) {
            if (type === 'year') {
              return `${value}年`;
            } else if (type === 'month') {
              return `${value}月`;
            }
            return value;
        },
        columns: ["男", "女"]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function () {
        this.getUserInfo()
    },

    getUserInfo: function () {
        wx.showLoading({
            title: "查询中...."
        })
        Ajax.get("/userInfos/findById", {}, sessionId)
            .then(res => {
                res.data.birthday = moment(res.data.birthday).format("YYYY-MM-DD");
                res.data.birth = new Date(res.data.birthday).getTime()
                res.data.newSex = res.data.sex === 1 ? "男" : "女"
                this.setData({
                    personInfo: res.data
                })
            })
            .catch(err => {
                throw err;
            })
            .finally(() => {
                wx.hideLoading()
            })
    },

    /**
     * 
     * 拼接地点函数
     */
    joinLoca: function (e) {
        let completeName = "";
        let param = e.detail.values;
        param.forEach(item => {
            completeName += `${item.name} `
        })
        return completeName;
    },

    newPhone: function() {
        this.setData({
            phoneShow: true
        })
    },

    newName: function() {
        this.setData({
            nameShow: true
        })
    },
    nameChange: function(e) {
        this.setData({realName: e.detail})
    },

    nameConfirm: function() {
        this.updateUser({realName: this.data.realName})
    },
    phoneChange: function(e) {
        this.setData({phone: e.detail})
    },

    phoneConfirm: function() {
        this.updateUser({phoneNo: this.data.phone})
    },
    sexConfirm: function(e) {
        this.setData({
            sex: e.detail.value,
            sexShow: false,
        }, () => {
            this.updateUser({sex: e.detail.value === "女" ? 2 : 1})
        })
    },
    chooseLocation: function () {
        this.setData({
            locationShow: true
        })
    },

    chooseSex: function() {
        this.setData({sexShow: true})
    },
    chooseBirth: function() {
        this.setData({show: true})
    },

    birthConfirm: function(e) {
        this.setData({
            birthday: moment(e.detail).format("YYYY-MM-DD"), 
            show: false
        }, () => {
            this.updateUser({birthday: e.detail})
        })
    },

    updateUser: function({
        birthday,
        phoneNo,
        realName,
        sex,
        userAddressCode,
    }) {
        const param = {
            birthday,
            phoneNo,
            realName,
            sex,
            userAddressCode,
        };
        Ajax.post("/userInfos/updateUserInfo", param, sessionId)
            .then(res => {
                if(res.code !== "0") {
                    wx.showToast({
                      title: res.message,
                      icon: false
                    })
                }
            })
            .catch(err => {
                throw err;
            })
    },
    /**
     * 
     * 设置参展地点
     */
    setLocation: function (e) {
        const result = this.joinLoca(e);
        this.setData({
            location: result,
            locationShow: false,
        }, () => {
            this.updateUser({userAddressCode: e.detail.values[2].code})
        })
    },
})